package yxy.game.pm2.active.command.monthfund;

import org.joda.time.DateTime;
import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actmonthfund;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_超值月基金;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.MonthFundInfo;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActMonthFundDrawRewardRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

@MessageType(proto = Command.ActMonthFundDrawReward, classtype = ActiveMessage.ActMonthFundDrawRewardRequest.class)
public class CMD_Active_超值月基金_DrawReward extends Command<ActMonthFundDrawRewardRequest> {

	private static final Logger logger = Logger.create(CMD_Active_超值月基金_DrawReward.class);
	
	@Override
	public void execute(MessageSource messageSource, ActMonthFundDrawRewardRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActMonthFundDrawRewardResponse.Builder response = ActiveMessage.ActMonthFundDrawRewardResponse.newBuilder();
		int type = request.getType();
		
		ActiveModule_超值月基金 am = ActiveModule_超值月基金.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		MonthFundInfo mf = am.getMonthFundByType(type);
		if(mf == null) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}
		
		int logindays = mf.getLogindays();
		ArrayList<Integer> getids = mf.getGetids();
		
		ArrayList<Integer> cangetids = new ArrayList<Integer>();
 		Map<String, actmonthfund> amfmap = ActDataCenter.getAllData(actmonthfund.class, playercode);
		for(actmonthfund amf : amfmap.values()) {
			if(amf.getType() != type) {
				continue;
			}
			
			if(amf.getDay() > logindays) {
				continue;
			}
			
			if(getids.contains(amf.getId())) {
				continue;
			}
			
			cangetids.add(amf.getId());
		}
		
		if(cangetids.size() == 0) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}
		
		int mincangetid = Collections.min(cangetids);
		actmonthfund amf = amfmap.get(String.valueOf(mincangetid));
		mf.getGetids().add(mincangetid);
		mf.setDrawtime(DateTime.now());
		am.set();
		
		Currency cur = Currency.create(amf.getRewardtype(), amf.getRewardid(), amf.getRewardnum());
		new CurrencyHandle().addCurrency(playercode, CCReason.活动_超值月基金, cur);
		
		PackageMessage.Item item = new CurrencyHandle().packItem(cur);
		
		response.addDisplay(item);
		send(messageSource, response, Result.Success);
	}
	
	private void send(MessageSource messageSource, ActiveMessage.ActMonthFundDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}



}
